package com.china08.yxyapi.service.manage.tec.teaching.zujuan.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.zujuan.IntellComponentQuestionService;
import com.china08.yxyapi.util.YxyResUtil;

@Service
public class IntellComponentQuestionServiceImpl implements IntellComponentQuestionService {
	@Autowired
	private ElasticsearchTemplate et;
	
	@Autowired
	private YxyResUtil yxyResUtil;

	@Autowired
	private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

	@Override
	public List<Long> getQuestionTypeAllCount(List<String> bookContentIdList, String difficultyLevel1) {	
		List<String> bookcontentListAll = new ArrayList<String>();
		for (String bc : bookContentIdList) {
			List<String> bookcontentList = yxyResUtil.findTocByTocParent(bc);
			bookcontentListAll.addAll(bookcontentList);
		}
        List<YxyResBookContentsKnoledges> bookContentsKnoledges = yxyResBookContentsKnoledgesRepository
                .findByBookContentIdIn(bookcontentListAll);
        List<Integer> knowIds = new ArrayList<Integer>();
        if (bookContentsKnoledges.size() > 0) {
            knowIds = bookContentsKnoledges.stream().map(x -> x.getKnowledgeId())
                    .collect(Collectors.toList());
        }
		// 查询当前所选的章节对应的各种类型题的数量
		List<Long> allCount = new ArrayList<Long>();

		// 1单选题的数量
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		// 查询知识点所有的id
		TermsQueryBuilder knowledgeIds = new TermsQueryBuilder("knowFullIds", knowIds);
		boolQueryBuilder.must(knowledgeIds);
		// 查询题的难易程度
		TermQueryBuilder difficultyLevel = new TermQueryBuilder("difficultyLevel", difficultyLevel1);
		boolQueryBuilder.must(difficultyLevel);
		// 单选题
		TermQueryBuilder questionType = new TermQueryBuilder("questionType", "1");
		boolQueryBuilder.must(questionType);
		TermQueryBuilder viewScopeBuilder = new TermQueryBuilder("viewScope", 0);
        boolQueryBuilder.mustNot(viewScopeBuilder);  
		SearchQuery query = new NativeSearchQueryBuilder().withFilter(boolQueryBuilder).build();

		// 查询出来的单选题的总数量
		long singleAllCount = et.count(query, ElasticYxyYxyQuesResource.class);
		allCount.add(singleAllCount);

		// 2多选题的数量
		BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
		// 查询知识点所有的id
		TermsQueryBuilder knowledgeIds2 = new TermsQueryBuilder("knowFullIds", knowIds);
		boolQueryBuilder2.must(knowledgeIds2);
		// 查询题的难易程度
		TermQueryBuilder difficultyLevel2 = new TermQueryBuilder("difficultyLevel", difficultyLevel1);
		boolQueryBuilder2.must(difficultyLevel2);
		// 多选题
		TermQueryBuilder questionType2 = new TermQueryBuilder("questionType", "8");
		boolQueryBuilder2.must(questionType2);
		boolQueryBuilder2.mustNot(viewScopeBuilder); 
		SearchQuery query1 = new NativeSearchQueryBuilder().withFilter(boolQueryBuilder2).build();

		// 查询出来的多选题的总数量
		long moreAllCount = et.count(query1, ElasticYxyYxyQuesResource.class);
		allCount.add(moreAllCount);

		// 3填空题的数量
		BoolQueryBuilder boolQueryBuilder3 = new BoolQueryBuilder();
		// 查询知识点所有的id
		TermsQueryBuilder knowledgeIds3 = new TermsQueryBuilder("knowFullIds", knowIds);
		boolQueryBuilder3.must(knowledgeIds3);
		// 查询题的难易程度
		TermQueryBuilder difficultyLevel3 = new TermQueryBuilder("difficultyLevel", difficultyLevel1);
		boolQueryBuilder3.must(difficultyLevel3);
		// 填空题
		TermQueryBuilder questionType3 = new TermQueryBuilder("questionType", "2");
		boolQueryBuilder3.must(questionType3);
		boolQueryBuilder3.mustNot(viewScopeBuilder); 
		SearchQuery query2 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder3).build();
		// 查询出来的填空题的总数量
		long blanksAllCount = et.count(query2, ElasticYxyYxyQuesResource.class);
		allCount.add(blanksAllCount);

		// 4解答题的数量
		BoolQueryBuilder boolQueryBuilder4 = new BoolQueryBuilder();
		// 查询知识点所有的id
		TermsQueryBuilder knowledgeIds4 = new TermsQueryBuilder("knowFullIds", knowIds);
		boolQueryBuilder4.must(knowledgeIds4);
		// 查询题的难易程度
		TermQueryBuilder difficultyLevel4 = new TermQueryBuilder("difficultyLevel", difficultyLevel1);
		boolQueryBuilder4.must(difficultyLevel4);
		// 解答题
		TermQueryBuilder questionType4 = new TermQueryBuilder("questionType", "1");
		boolQueryBuilder4.mustNot(questionType4);
		TermQueryBuilder questionType41 = new TermQueryBuilder("questionType", "8");
		boolQueryBuilder4.mustNot(questionType41);
		TermQueryBuilder questionType42 = new TermQueryBuilder("questionType", "2");
		boolQueryBuilder4.mustNot(questionType42);
		boolQueryBuilder4.mustNot(viewScopeBuilder); 
		SearchQuery query4 = new NativeSearchQueryBuilder().withFilter(boolQueryBuilder4).build();
		// 查询出来的解答题的总数量
		long answerAllCount = et.count(query4, ElasticYxyYxyQuesResource.class);
		allCount.add(answerAllCount);
		return allCount;
	}
}
